<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Hash;

class LoginController extends Controller
{
    //后台登录页面
    public function getIndex()
    {
    	//解析模板
    	return view('admin.login');
    	// return view('home.user.index');
    }


    //后台登录处理
    public function postDologin(Request $request)
    {
    	// dd($request->all());
    	//检测用户名是否存在
    	$user = DB::table('users')
    	->where('username',$request->input('username'))
    	->first();
    	// dd($user);

    	if(!empty($user)){
    		//检测密码是否一致
    		if(Hash::check($request->input('password'),$user->password)){
    			//登录成功
    			session(['id'=>$user->id,'username'=>$user->username]);
    			//跳转页面
    			return redirect('/admin')->with('success','欢迎'.$user->username.'登录...');
    		}else{
    			return back()->with('error','用户名或者密码错误');
    		}

    	}else{
    		return back()->with('error','用户名不存在');
    	}
    }

    //后台退出
    public function getLogout()
    {
    	session()->forget('id','username');
    	return redirect('/admin/login')->with('success','安全退出成功');
    }


    //前台注册
    public function register()
    {
    	return view('home.register');
    }
}
